/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package db.definitions;

/**
 *
 * @author abracadabuda
 */
public class FunctionDefinition extends AbstractDefinition {
    private String name;
    private String parameters;
    private String returnStat;
    private String body;
    private String lang;
    private String rest;    
    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getParameters() {
        return parameters;
    }

    public void setParameters(String parameters) {
        this.parameters = parameters;
    }

    public String getReturnStat() {
        return returnStat;
    }

    public void setReturnStat(String returnStat) {
        this.returnStat = returnStat;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body.replaceAll("%%%.", "\"" + prefix + "\"");
    }

    public String getLang() {
        return lang;
    }

    public void setLang(String lang) {
        this.lang = lang;
    }

    public String getRest() {
        return rest;
    }

    public void setRest(String rest) {
        this.rest = rest;
    }

    
    /**
     * gets the SQL code of function creation
     * @return SQL code
     */
    public String getSQL() {
        String sql = "CREATE OR REPLACE FUNCTION \"" + prefix + "\".\"" + name + "\" (" + parameters +")\n";
        sql += "RETURNS " + returnStat + " AS\n";
        sql += "$BODY$\n" + body + "\n$BODY$\n";
        sql += lang.equals("") ? "" : "LANGUAGE " + lang + "\n" + rest;
        return sql + ";\n";
    }
    
}
